package v1

import (
	"saas_sys_base/lib/lbsqq/util"
	"strconv"
)

func Matrix(clt *util.Client, request MatrixParam) (matrixResonse *MatrixResonse, err error) {
	var params = getParams(request)
	var incompleteURL string = "http://apis.map.qq.com/ws/distance/v1/matrix"

	if err = clt.GetJSON(incompleteURL, params, &matrixResonse); err != nil {
		return
	}

	return
}

func getParams(request MatrixParam) map[string]string {
	var params = make(map[string]string)
	params["mode"] = request.Mode
	params["from"] = strconv.FormatFloat(request.From.Lat, 'f', -1, 64) + "," + strconv.FormatFloat(request.From.Lng, 'f', -1, 64)

	var toStr string = ""
	var firstTo = true
	for _, p := range request.To {
		if firstTo {
			toStr = strconv.FormatFloat(p.Lat, 'f', -1, 64) + "," + strconv.FormatFloat(p.Lng, 'f', -1, 64)
		} else {
			toStr += ";" + strconv.FormatFloat(p.Lat, 'f', -1, 64) + "," + strconv.FormatFloat(p.Lng, 'f', -1, 64)
		}
		firstTo = false
	}
	params["to"] = toStr
	params["key"] = request.Key
	return params
}
